        if (savedItems) {
            JSON.parse(savedItems).forEach(item => {
                createListItem(item.text, item.member, item.status);
            });
        }
    }
});
    // 从本地存储加载数据
    loadItems();
    
    // 添加物品
    addBtn.addEventListener('click', addItem);
    itemInput.addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            addItem();
        }
    });
    
    function addItem() {
        const itemText = itemInput.value.trim();
        const selectedMember = memberSelect.options[memberSelect.selectedIndex].text;
        
        if (itemText) {
            createListItem(itemText, selectedMember);
            itemInput.value = '';
            saveItems();
        }
    }
    
    function createListItem(text, member, status = 'pending') {
        const li = document.createElement('li');
        
        const itemInfo = document.createElement('div');
        itemInfo.className = 'item-info';
        
        const span = document.createElement('span');
        span.textContent = text;
        
        const memberSpan = document.createElement('span');
        memberSpan.className = 'item-member';
        memberSpan.textContent = `(${member})`;
        
        const statusControls = document.createElement('div');
        statusControls.className = 'item-status';
        
        const buyBtn = document.createElement('button');
        buyBtn.className = 'status-btn buy-btn';
        buyBtn.textContent = '已购买';
        buyBtn.addEventListener('click', function() {
            li.dataset.status = 'bought';
            saveItems();
        });
        
        const pendingBtn = document.createElement('button');
        pendingBtn.className = 'status-btn pending-btn';
        pendingBtn.textContent = '待付款';
        pendingBtn.addEventListener('click', function() {
            li.dataset.status = 'pending';
            saveItems();
        });
        
        const deleteBtn = document.createElement('button');
        deleteBtn.className = 'status-btn delete-btn';
        deleteBtn.textContent = '删除';
        deleteBtn.addEventListener('click', function() {
            li.remove();
            saveItems();
        });
        
        itemInfo.appendChild(span);
        itemInfo.appendChild(memberSpan);
        
        statusControls.appendChild(buyBtn);
        statusControls.appendChild(pendingBtn);
        statusControls.appendChild(deleteBtn);
        
        li.appendChild(itemInfo);
        li.appendChild(statusControls);
        li.dataset.status = status;
        
        shoppingList.appendChild(li);
    }
    
    function saveItems() {
        const items = [];
        document.querySelectorAll('#shoppingList li').forEach(li => {
            items.push({
                text: li.querySelector('span').textContent,
                completed: li.classList.contains('completed')
            });
        });
        localStorage.setItem('shoppingList', JSON.stringify(items));
    }
    
    function loadItems() {
        const savedItems = localStorage.getItem('shoppingList');
        if (savedItems) {
            JSON.parse(savedItems).forEach(item => {
                createListItem(item.text, item.completed);
            });
        }
    }
    
    // 模拟实时更新（实际项目中应该使用WebSocket或类似技术）
    setInterval(function() {
        console.log("检查更新...");
        // 这里可以添加实际的实时同步逻辑
    }, 5000);
});

